#define _CRT_SECURE_NO_WARNINGS

#include <Windows.h>
#include <iostream>
#include <time.h>
#include <mmsystem.h>

#include "SuperString.h"
#include "resource.h"

//Defines for child controls
#define PBUTTON	90000
#define CBUTTON	90001
#define RBUTTON	90002
#define SBUTTON	90003
#define LISTBOX	90004
#define EDCNTRL	90005

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, PSTR lpszCmdParam, int nCmdShow ) {
	static char	szAppName[] = "Wesley Maxey - Lab 5";
	HWND		hwnd;
	MSG			msg;
	WNDCLASSEX	wndclass;

	wndclass.cbSize			= sizeof(wndclass);
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= WndProc;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInst;
	wndclass.hIcon			= LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON1) );
	wndclass.hCursor		= LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground	= WHITE_BRUSH;
	wndclass.lpszMenuName	= NULL;
	wndclass.lpszClassName	= szAppName;
	wndclass.hIconSm		= LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON1) );

	RegisterClassEx (&wndclass);

	hwnd = CreateWindow (szAppName,	// window class name
		"Maxey - Lab 5",			// window caption
		WS_OVERLAPPEDWINDOW,		// window style
		CW_USEDEFAULT,				// initial x position
		CW_USEDEFAULT,				// initial y position
		1200,						// initial x size
		800,						// initial y size
		NULL,						// parent window handle
		NULL,						// window menu handle
		hInst,						// program instance handle
		NULL);						// creation parameters

	ShowWindow (hwnd, nCmdShow);
	UpdateWindow (hwnd);

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}
	return (int)(msg.wParam);
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	HDC				hdc, hbdc;
	PAINTSTRUCT		ps;
	RECT			rect;
	RECT			trect;
	int				errorcheck;
	
	static HWND		hpbutton;
	static HWND		hscontrol;
	static HWND		hrbutton;
	static HWND		hedcntrl;
	static HWND		hlscntrl;
	static HMENU	hmenu;

	static HINSTANCE hinst;

	switch (message)
	{
	case WM_COMMAND:
		switch ( wParam )
		{
		case ID_QUIT:
			SendMessage( hwnd, WM_CLOSE, NULL, NULL );
			break;
		case PBUTTON:
			MessageBox( hwnd, "Got it!", "Push button pressed!", MB_OK );
			break;
		}
		break;
	case WM_CREATE:
		hinst = (HINSTANCE)( GetWindowLong( hwnd, GWL_HINSTANCE ) );

		hmenu = LoadMenu( NULL, MAKEINTRESOURCE( IDR_MENU1 ) );

		hpbutton = CreateWindow( "button", "PushButton", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
			100, 5, 400, 25, hwnd, (HMENU)PBUTTON, hinst, NULL );

		errorcheck = GetLastError();

		hrbutton = CreateWindow( "button", "RadioButton", WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
			100, 45, 400, 55, hwnd, (HMENU)RBUTTON, hinst, NULL );

		hscontrol = CreateWindow( "static", "StaticControl", WS_CHILD | WS_VISIBLE | SS_CENTER, 
			100, 85, 400, 105, hwnd, (HMENU)SBUTTON, hinst, NULL );

		hedcntrl = CreateWindow( "edit", "EditControl", WS_CHILD | WS_VISIBLE | ES_CENTER,
			100, 135, 400, 155, hwnd, (HMENU)EDCNTRL, hinst, NULL );

		hlscntrl = CreateWindow( "ListBox", "ListBox", WS_CHILD | WS_VISIBLE | LBS_STANDARD,
			100, 185, 400, 205, hwnd, (HMENU)LISTBOX, hinst, NULL );

		SendMessage( hlscntrl, LB_DIR, DDL_DRIVES, (LPARAM)("") );

		SetMenu( hwnd, hmenu );
		return 0;

	case WM_PAINT:
		break;

	case WM_SIZE:
	case WM_MOVE:
		GetClientRect(hwnd, &rect);
		InvalidateRect( hwnd, &rect, false );
		return 0;

	case WM_CHAR:
		return 0;

	case WM_QUIT:
		break;

	case WM_DESTROY:
		DestroyWindow( hpbutton );
		DestroyWindow( hrbutton );
		DestroyWindow( hscontrol );
		DestroyWindow( hedcntrl );
		DestroyWindow( hlscntrl );
		DestroyMenu( hmenu );
		PostQuitMessage(0);

		return 0;

	case WM_MOUSEMOVE:
		return 0;

	case WM_KEYDOWN:
		break;
	}

	return DefWindowProc (hwnd, message, wParam, lParam);
}